﻿using CKS.PLC.Core.Event;
using CKS.Schedule;
using Infrastructure;
using Infrastructure.Extensions;
using Org.BouncyCastle.Asn1.Ocsp;
using RestSharp;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZR.Model.Dto.Business.ProductionControl;
using ZR.Model.Models.ProductionControl;
using ZR.Model.System;

namespace CKS.PLC.Core.CronJob
{
    [Schedule]
    public class ApiJob : CronJobBase
    {
        public ApiJob() : base("ApiJob", "0/10 * * * * ?")
        {
        }

        protected override async Task ExecuteAsync(CancellationToken cancellationToken)
        {
            EventBus.AddLog("触发");
            var cmmInfo = await InternalApp.Fsql.Select<CksCMMProduct>().Where(x => !x.Uploaded).ToOneAsync();
            var deviceApiInfo = await InternalApp.Fsql.Select<SysDictData>().Where(x => x.DictType == "cks_cmm" && x.DictLabel == ("Device" + cmmInfo.DeviceId)).ToOneAsync();
            RestClient restClient = new RestClient(deviceApiInfo.DictValue);
            var request = new RestRequest(deviceApiInfo.LangKey, Method.Get);
            var apiResult = await restClient.ExecuteGetAsync<CksCmmDto>(request);
            if (cmmInfo.IsNotEmpty() && apiResult.IsSuccessful && apiResult.Data != null)
            {
                string gid = $"{Guid.NewGuid().ToString("N")}";
                var cmmList = apiResult.Data;
                CksCMMInspectionRecord cmmIR = new CksCMMInspectionRecord()
                {
                    BatchCode = gid,
                    F1= "Device" + cmmInfo.DeviceId,
                    ProductId = cmmList.Parameters.Find(x=>x.Name=="SN").Value,
                    EID = cmmList.EID,
                    CheckResult = cmmList.CheckResult,
                    StartTime = DateTimeOffset.FromUnixTimeSeconds(cmmList.StartTime).LocalDateTime,
                    EndTime = DateTimeOffset.FromUnixTimeSeconds(cmmList.EndTime).LocalDateTime,
                    CostTime = cmmList.CostTime,
                    CreateTime = DateTime.Now
                };
                var cmmIP = cmmList.Parameters.Select(x => new CksCMMInspectionParameter()
                {
                    BatchCode = gid,
                    Name = x.Name,
                    Value = x.Value,
                    CreateTime = DateTime.Now
                }).ToList();
                var cmmII = cmmList.Items.Select(x => new CksCMMInspectionItem()
                {
                    BatchCode = gid,
                    CheckResult = x.CheckResult,
                    Idsymbol = x.Idsymbol,
                    StandardValue = x.StandardValue,
                    ItemName = x.ItemName,
                    ActualValue = x.ActualValue,
                    Unit = x.Unit,
                    LSL = x.LSL,
                    USL = x.USL,
                    CreateTime = DateTime.Now
                }).ToList();
                var callbackId = await InternalApp.Fsql.Insert(cmmIR).ExecuteIdentityAsync();
                await InternalApp.Fsql.Insert(cmmIP).ExecuteIdentityAsync();
                await InternalApp.Fsql.Insert(cmmII).ExecuteIdentityAsync();
                if (callbackId > 0)
                {
                    cmmInfo.Uploaded = true;
                    InternalApp.Fsql.Update<CksCMMProduct>().SetSource(cmmInfo).ExecuteAffrows();
                }
            }
            await Task.CompletedTask;
        }
    }
}